import Types from '../actionTypes';

import {
  getCodeLibraryInfo,
  getAllBrand,
  getSingleCode
} from '../../api';

const CATE_CODE = (cate_code) => {
  return {
    type: Types.CATE_CODE,
    cate_code
  };
};

const GET_ALL_BRAND = () => {
  return new Promise((resolve, reject) => {
    getAllBrand().then(res => {
      resolve({
        type: Types.GET_ALL_BRAND,
        list: res.data
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

const IS_SELECTED = value => {
  return {
    type: Types.IS_SELECTED,
    value
  };
};

const CURRENT_PAGE = value => {
  return {
    type: Types.CURRENT_PAGE,
    value
  };
};

const GET_CODELIBRARY_INFO = (cate_code, page = 1, page_size = 10, search = '') => {
  return new Promise((resolve, reject) => {
    getCodeLibraryInfo(cate_code, page, page_size, search)
      .then(res => {
        resolve({
          type: Types.GET_CODELIBRARY_INFO,
          value: res
        });
      })
      .catch(err => {
        console.log(err);
      });
  });
};

// 获取单条数据
const GET_SINGLE_CODE = invcode => {
  if (invcode === 0) {
    return {
      type: Types.GET_SINGLE_CODE,
      data: {}
    };
  } else {
    return new Promise((resolve, reject) => {
      getSingleCode(invcode).then(res => {
        resolve({
          type: Types.GET_SINGLE_CODE,
          data: res.data
        });
      }).catch(err => {
        console.log(err);
      });
    });
  }
};

const IS_MODIFY = boobean => {
  return {
    type: Types.IS_CODELIBRARY_MODIFY,
    boobean
  };
};

export default {
  CATE_CODE,
  GET_ALL_BRAND,
  IS_SELECTED,
  CURRENT_PAGE,
  GET_CODELIBRARY_INFO,
  IS_MODIFY,
  GET_SINGLE_CODE
};
